<?php
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
class Picture extends ActiveRecord
{
    public static function db() {
        return Yii::$app->db;
    }
    
    public static function tableName() {
        return 'house_pics';
    }

    /**
     * 收藏他人图片
     * @param array $data
     */
    public function collectPics($data) {
        return self::db()->createCommand()
            ->insert('house_pics_list', [
                'uid'      => $data['uid'],
                'ideaid'   => $data['ideaid'],
                'picsid'   => $data['picsid']
            ])->execute();
    }
    /**
     * 修改灵感
     * @param unknown $data
     */
    public function updatePics($data) {
        return self::db()->createCommand()
            ->update(self::tableName(), [
                'description'   => $data['desc'],
                ],
                ['id' => $data['picsid']]
            )->execute();
    }
    /**
     * 删除某个灵感里单个图片
     * @param int $ideaid
     */
    public function deleteImg($ideaid, $picsid){
        return self::db()->createCommand()->delete('house_pics_list', 'ideaid='.$ideaid .' and picsid in('.$picsid.')')->execute();
    }
    /**
     * 获取自己灵感里面的内容
     * @param array $data
     */
    public function getImgList($data){
        $limit = ($data['page'] -1) * $data['size'];
        $sql   = 'SELECT * FROM house_pics_list WHERE ideaid=' . $data['ideaid'] . ' limit '. $limit . ',' . $data['size'];
        return self::db()->createCommand($sql)->queryAll();
    }
    /**
     * 通过图片素材id获取图片id
     * @param int $picsid
     */
    public function getImg($picsid){
        $sql  = 'SELECT imgid FROM house_pics WHERE id=' . $picsid;
        return self::db()->createCommand($sql)->queryScalar();
    }
}
